
#实例化calculate类
import time
from typing import List
import pytest
from pythoncode.calculate import Calculate

@pytest.fixture(scope="class")
def get_calc():
    print("开始计算")
    calc = Calculate()
    yield calc
    print("计算结束")

#修改测试用例名称中文编码,utf-8转换为unicode
def pytest_collection_modifyitems(
        session: "Session", config: "Config", items: List["Item"]
) -> None:
    for item in items:
        item.name = item.name.encode('utf-8').decode('unicode-escape')
        item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')


#自动生成日志文件，文件名以时间命名
@pytest.fixture(scope="session", autouse=True)
def manage_logs(request):
    """Set log file name same as test name"""
    #获取根目录
    rootdir = request.config.rootdir
    print(rootdir)
    now = time.strftime("%Y-%m-%d %H-%M-%S")
    log_name =  rootdir + '/output/log/test' + now + '.logs'

    request.config.pluginmanager.get_plugin("logging-plugin") \
        .set_log_path(log_name)
